﻿using DBUtil.Attributes;
using NUnit.Framework;
using Shouldly;
using System;
using System.ComponentModel.DataAnnotations.Schema;

namespace Test.MySql.Inserts
{
    [TestFixture]
    internal class InsertFromSelect : TestBase
    {
        #region model
        [Table("test")]
        public class Person
        {
            [PrimaryKey, Identity]
            [Column("id", Order = 1)]
            public int Id { get; set; }

            [Column("name", Order = 3)]
            public string Name { get; set; }

            [Column("age", Order = 2)]
            public int Age { get; set; }
        }

        [Table("test_json")]
        public class PersonJson
        {
            [PrimaryKey, Identity]
            [Column("id")]
            public int Id { get; set; }

            [Column("name")]
            public string Name { get; set; }

            [JsonMap]
            [Column("ext2")]
            public Person Leader { get; set; }
        }
        public enum EnumTest
        {
            None, Active, NoActive
        }
        #endregion

        [SetUp]
        public void SetUp()
        {
            DropTableIfExist("test");
            db.ExecuteSql("create table test(id int primary key auto_increment,name varchar(50),age int)");
            db.ExecuteSql("insert into test(name,age) values('jack',20),('tom',18);");

            DropTableIfExist("test_json");
            db.ExecuteSql("create table test_json(id int primary key auto_increment,name varchar(50),ext2 json)");
            db.ExecuteSql("insert into test_json(name,ext2) values('jack','{\"id\":2,\"name\":\"ja\",\"age\":16}'),('tom','{\"id\":3,\"name\":\"to\",\"age\":18}');");
        }

        [Test]
        public void Simple()
        {
            var sql = db.Select<Person>().Where(i => i.Age > 10).AsInsert(i => new Person
            {
                Name = i.Name,
                Age = i.Age + 10
            }).ToSql();
            sql.ShouldBe("""
                insert into `test`(`name`,`age`) select t.`name` `Name`,t.`age` + 10 `Age`
                from `test` t
                where t.`age` > 10;
                """);

            try
            {
                db.Select<Person>().Where(i => i.Age > 10).AsInsert(i => i).ToSql();
            }
            catch (Exception ex)
            {
                ex.Message.ShouldContain("不能在 AsInsert 方法中使用");
            }
        }

        [Table("t_person_json")]
        public class PersonJson2
        {
            public int Id { get; set; }
            [JsonMap]
            public ExtInfo Ext { get; set; }
        }
        public class ExtInfo
        {
            public int Age { get; set; }
            public string Name { get; set; }
        }
        [Test]
        public void SimpleJson()
        {
            var sql = db.Select<PersonJson2>().Where(i => i.Ext.Age > 18).AsInsert(i => new Person
            {
                Id = i.Id,
                Name = i.Ext.Name,
                Age = i.Ext.Age,
            }).ToSql();
            sql.ShouldBe("""
                insert into `test`(`id`,`name`,`age`) select t.`Id`,json_value(t.`Ext`,'$."Name"' returning char) `Name`,json_value(t.`Ext`,'$."Age"' returning signed) `Age`
                from `t_person_json` t
                where (json_value(t.`Ext`,'$."Age"' returning signed)) > 18;
                """);
        }
    }
}
